#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

SURICATA_DIR=${SURICATA_DIR:-/etc/suricata}
SURICATA=${SVCNAME#*.}
if [ -n "${SURICATA}" ] && [ ${SVCNAME} != "suricata" ]; then
    SURICATACONF="${SURICATA_DIR}/suricata-${SURICATA}.yaml"
    SURICATAPID="/var/run/suricata/suricata.${SURICATA}.pid"
else
    SURICATACONF="${SURICATA_DIR}/suricata.yaml"
    SURICATAPID="/var/run/suricata/suricata.pid"
fi
SURICATAOPTS=${SURICATA_OPTIONS}

extra_commands="checkconfig"
extra_started_commands="reload"

depend() {
	need net
	after firewall
}

checkconfig() {
	if [ ! -e ${SURICATACONF} ] ; then
		eerror "You need to create ${SURICATACONF} to run ${SVCNAME}."
		return 1
	fi
}

start() {
	checkconfig || return 1
	ebegin "Starting ${SVCNAME}"
	start-stop-daemon --start --quiet --exec /usr/bin/suricata \
		-- --pidfile ${SURICATAPID} -D ${SURICATAOPTS} \
        -c ${SURICATACONF} >/dev/null 2>&1
	eend $?
}

stop() {
	ebegin "Stopping ${SVCNAME}"
	start-stop-daemon --stop --quiet --pidfile ${SURICATAPID} >/dev/null 2>&1
	einfon "Waiting for ${SVCNAME} to shut down. This can take a while..."
	echo
	# max wait: 5 minutes as it can take quite a while on some systems with heavy traffic
	cnt=300
	while [ -f ${SURICATAPID} ]; do
	    cnt=$(expr $cnt - 1)
	    if [ $cnt -lt 1 ] ; then
		echo
		eend 1 "Failed."
		break
	    fi
	    sleep 1
	    echo -ne "$cnt seconds left before we give up\r"
	done
	eend $?
}

reload() {
    checkconfig || return 1
	ebegin "Reloading ${SVCNAME}"
	start-stop-daemon --signal HUP --pidfile ${SURICATAPID}
}
